package com.open.capacity.form.controller;


import com.open.capacity.annotation.log.LogAnnotation;
import com.open.capacity.commons.PageResult;
import com.open.capacity.commons.Result;
import com.open.capacity.form.model.FormRelation;
import com.open.capacity.form.service.FormRelationService;
import com.open.capacity.form.vo.FormRelationVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @author 作者 ftquan 464725602@qq.com
 * @version 创建时间：2017年11月12日 上午22:57:51
 * 表单关联
 */
@RestController
@Api(tags = "表单关联信息模块api")
public class FormRelationController {


    @Autowired
    private FormRelationService formRelationService;


    /**
     * 查询表单关联
     *
     * @param params
     * @return
     */
    @PreAuthorize("hasAuthority('formrelation:get/formrelations')")
    @ApiOperation(value = "查询表单关联")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "分页起始位置", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "limit", value = "分页结束位置", required = true, dataType = "Integer")
    })
    @GetMapping("/formrelations")
    @LogAnnotation(name = "表单",module = "form-center", recordRequestParam = false)
    public PageResult<FormRelation> findFormRelations(@RequestParam Map<String, Object> params) {
        return formRelationService.findRelations(params);
    }



    @ApiOperation(value = "获取表单关联")
    @PreAuthorize("hasAuthority('formrelation:get/formrelations/{id}')")
    @GetMapping("/formrelations/{id}")
    public Result<FormRelationVo> findById(@PathVariable(value = "id") String id) {
        FormRelationVo relation = formRelationService.findById(id);
        return Result.succeed(relation);
    }


    /**
     * 表单关联新增或者更新
     *
     * @param relation
     * @param keyValue
     * @return
     */
    @PreAuthorize("hasAnyAuthority('formrelation:post/formrelations','formrelation:put/formrelations')")
    @ApiOperation(value = "保存表单关联")
    @PostMapping("/formrelations/saveOrUpdate")
    @LogAnnotation(name = "表单",module = "form-center", recordRequestParam = false)
    public Result saveOrUpdate(@RequestBody FormRelationVo relation, String keyValue) {
        return formRelationService.saveOrUpdate(relation, keyValue);
    }


    /**
     * 删除
     * delete /formrelations/1
     *
     * @param id
     */
    @PreAuthorize("hasAuthority('formrelation:delete/formrelations/{id}')")
    @ApiOperation(value = "删除表单")
    @DeleteMapping("/formrelations/{id}")
    @LogAnnotation(name = "表单",module = "form-center", recordRequestParam = false)
    public Result delete(@PathVariable(value = "id") String id) {
        try {
            formRelationService.delete(id);
            return Result.succeed("操作成功");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.failed("操作失败");
        }
    }

}
